/*
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.apidoc.handler;

import java.util.*;
import java.util.stream.Collectors;

import com.hyts.apidoc.model.ApiDefination;
import com.hyts.apidoc.model.ApiDoc;
import com.hyts.apidoc.servlet.ApiDocServlet;
import com.hyts.apidoc.utility.StringUtils;

/**
 * @title TemplateHandler.java
 * @package com.hyts.apidoc.template
 * @description TODO
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com
 * @date 2018年4月23日
 * @version V1.0
 */
public class TemplateDataHandler {

    /**
     * 统计apidoc的package包的数据结构 @exception
     */
    public static Map<String, List<ApiDoc>> countApiDocList() {
        // 校验数据是否存在
        if (ApiDocServlet.getApiDocList() == null || ApiDocServlet.getApiDocList().isEmpty()) {
            System.err.println("无法进行相关apidoc的操作因为解析到的apiDocList数据为空!");
            return null;
        }
        // 操作apidoc服务功能
        System.out.println("------------------"+ApiDocServlet.getApiDocList());
        Map<String, List<ApiDoc>> data = ApiDocServlet.getApiDocList().stream()
                .collect(Collectors.groupingBy(ApiDoc::getPackageName));
        System.out.println("------------------"+data);
        /*data.forEach((k,v)->{
          k.replace(ApiDocConfiguration.annotationBasePackage+".","");
        });*/
       /* Stream.of(data).collect(Collectors.toMap((k)->k.toString().
                replace(ApiDocConfiguration.annotationBasePackage,""),v->v));
        System.out.println(data);*/
        return data;
    }

    /**
     * <b>获取所有相关的package不为空的集合</b> @return @exception
     */
    public static List<ApiDoc> getPackageNotNull() {
        if (ApiDocServlet.getApiDocList() == null || ApiDocServlet.getApiDocList().isEmpty()) {
            System.err.println("无法进行相关apidoc的操作因为解析到的apiDocList数据为空!");
            return null;
        }
        return ApiDocServlet.getApiDocList().stream()/*
                                                      * . map(param->param.getPackageName())
                                                      */.filter(param -> StringUtils.isNotBlank(param.getPackageName()))
                .collect(Collectors.toList());
    }

    /**
     * <b>获取所有相关的package为空的集合</b> @return @exception
     */
    public static List<ApiDoc> getPackageNull() {
        // 判断是否为空
        if (ApiDocServlet.getApiDocList() == null || ApiDocServlet.getApiDocList().isEmpty()) {
            System.err.println("无法进行相关apidoc的操作因为解析到的apiDocList数据为空!");
            return null;
        }
        return ApiDocServlet.getApiDocList().stream()/*
                                                      * . map(param->param.getPackageName())
                                                      */.filter(param -> StringUtils.isBlank(param.getPackageName()))
                .collect(Collectors.toList());
    }

    /**
     * @return @exception
     */
    public static List<ApiDoc> getClassNameNotNull() {
        // 判断是否为空
        if (ApiDocServlet.getApiDocList() == null || ApiDocServlet.getApiDocList().isEmpty()) {
            System.err.println("无法进行相关apidoc的操作因为解析到的apiDocList数据为空!");
            return null;
        }
        return ApiDocServlet.getApiDocList().stream()/*
                                                      * . map(param->param.getPackageName())
                                                      */.filter(param -> StringUtils.isNotBlank(param.getApiTypeName()))
                .collect(Collectors.toList());
    }

    /**
     * @return @exception
     */
    public static List<ApiDoc> getClassNameNull() {
        // 判断是否为空
        if (ApiDocServlet.getApiDocList() == null || ApiDocServlet.getApiDocList().isEmpty()) {
            System.err.println("无法进行相关apidoc的操作因为解析到的apiDocList数据为空!");
            return null;
        }
        return ApiDocServlet.getApiDocList().stream()/*
                                                      * . map(param->param.getPackageName())
                                                      */.filter(param -> StringUtils.isBlank(param.getApiTypeName()))
                .collect(Collectors.toList());
    }


    /**
     * @param apiDocList
     * @param classList
     * @exception
     */
    public static void injectPackageName(List<ApiDoc> apiDocList,List<Class<?>> classList) {
        apiDocList.stream().forEach(param->{
            classList.stream().forEach(param2->{
                String path = param.getApiTypeName();
                //重复名字且不同包会有问题
                if(StringUtils.isBlank(param.getPackageName())) {
                    if(path.contains(param2.getSimpleName()) || param2.getSimpleName().contains(path)
                            || path.equals(param2.getSimpleName())){
                        param.setPackageName(param2.getPackage().getName());
                    }else {
                        param.setPackageName("未定义包名!");
                    }
                }
            });
        });
    }

    /**
     * @param apiDocList
     * @exception
     */
    public static void injectClassName(List<ApiDoc> apiDocList) {
        apiDocList.stream().forEach(param->{
            if(StringUtils.isEmpty(param.getApiTypeName()))
                param.setApiTypeName("未定义类名称!");
        });
    }


    public static List<ApiDoc> getClassDataListByPackageName(String packageName) {
        if (ApiDocServlet.getApiDocList() == null || ApiDocServlet.getApiDocList().isEmpty()) {
            System.err.println("无法进行相关apidoc的操作因为解析到的apiDocList数据为空!");
            return Collections.emptyList();
        }
        return ApiDocServlet.getApiDocList().stream().filter(param->param.getPackageName().equals(packageName)).collect(Collectors.toList());
    }



    public static List getListByPageApidDoc(List<ApiDoc> dataList,int pageIndex){
        pageIndex = pageIndex <=0?1:pageIndex;
        int skipNum = (pageIndex-1) * ApiDocServlet.DEFAULT_PAGE_SIZE;
        return dataList.stream().skip(skipNum).limit(ApiDocServlet.DEFAULT_PAGE_SIZE)
                .collect(Collectors.toList());
    }


    public static List getListByPage(List<ApiDefination> dataList, int pageIndex){
        pageIndex = pageIndex <=0?1:pageIndex;
        int skipNum = (pageIndex-1) * ApiDocServlet.DEFAULT_PAGE_SIZE;
        return dataList.stream().skip(skipNum).limit(ApiDocServlet.DEFAULT_PAGE_SIZE)
                .collect(Collectors.toList());
    }


    /**
     * @param dataList
     * @return
     * @exception
     */
    public static int pageNum(List<?> dataList) {
        int num = (dataList.size() / ApiDocServlet.DEFAULT_PAGE_SIZE);
        return dataList.size() % ApiDocServlet.DEFAULT_PAGE_SIZE == 0 ? num:++num;
    }

    /**
     * @param pageNum 页总数-实际总数
     * @param pageIndex 页下标，当前请求的页下标
     * @return
     * @exception
     */
    public static List<Integer> getPageIndexList(int pageNum,int pageIndex){
        int start = 1;
        pageIndex = pageIndex <=0?1:pageIndex;
        pageNum = pageNum <=0?1:pageNum;
        int count = (pageNum>=pageIndex&&pageNum>ApiDocServlet.DEFAULT_PAGE_INDEX)
                ?ApiDocServlet.DEFAULT_PAGE_INDEX:pageNum;
        pageIndex = pageIndex == pageNum?pageIndex-1:pageIndex;
        int num = (pageIndex - ApiDocServlet.DEFAULT_PAGE_INDEX)+1;
        if(num >= 1) {
            start+=num;
            count+=num;
        }
        List<Integer> pageIndexList = new LinkedList<>();
        for(int i = start ; i <=count ; i++) {
            pageIndexList.add(i);
        }
        return pageIndexList;
    }


    public static void main(String[] args){
        System.out.println(getPageIndexList(10,7));
    }
}
